﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using SimpleNoteLib.ApiRequests;
using SimpleNoteLib.Exceptions;

namespace SimpleNoteLib
{
    [DataContract]
    public class Login
    {
        #region Fields

        private static string token;
        [DataMember]
        private static DateTime tokenCreatedOn;

        #endregion

        /// <summary>
        /// Stores login token
        /// </summary>
        /// <exception cref="WrongTokenException">Thrown if there is no login token set or it is out-dated</exception>
        [DataMember]
        public static string Token
        {
            get
            {
                TimeSpan diff = DateTime.Now - tokenCreatedOn;
                if (diff.TotalHours > 24)
                {
                    token = null;
                }

                if (string.IsNullOrWhiteSpace(token))
                {
                    throw new WrongTokenException("No Login Token or it is out-dated. Please login");
                }
                return token;
            }
            private set
            {
                token = value;
            }
        }

        /// <summary>
        /// Checks if Login token is still valid
        /// </summary>
        public bool IsTokenUpToDate
        {
            get
            {
                TimeSpan diff = DateTime.Now - tokenCreatedOn;
                return diff.TotalHours < 24.0d;
            }
        }

        [DataMember]
        public static string Email { get; set; }

        /// <summary>
        /// Logs user and returns token needed in other requests. Token is also saved in static 'Token' property
        /// </summary>
        /// <param name="email">E-email address used as login</param>
        /// <param name="password">User password</param>
        /// <returns>Login token</returns>
        public string LogUser(string email, string password)
        {
            LoginRequest request = new LoginRequest(email, password);
            string token = request.SendAndGetResponse();
            Token = token;
            Email = email;
            tokenCreatedOn = DateTime.Now;
            return token;
        }

        /// <summary>
        /// Logs user and returns token needed in other requests. Token is also saved in static 'Token' property
        /// </summary>
        /// <param name="email">E-email address used as login</param>
        /// <param name="password">User password</param>
        /// <param name="address">Login API address</param>
        /// <param name="proxy">Proxy</param>
        /// <returns>Login token</returns>
        public string LogUser(string email, string password, string address, IWebProxy proxy = null)
        {
            LoginRequest request = new LoginRequest(address, email, password, proxy);
            string token = request.SendAndGetResponse();
            Token = token;
            Email = email;
            tokenCreatedOn = DateTime.Now;
            return token;
        }
    }
}
